<template>
	<view class="page" :style="'height:' + height + 'rpx'">
		<wareItem v-for="(item,index) in list" :key="index" :item="item" class="list_item" @toDetail="toDetail()"></wareItem>
	</view>
</template>

<script>
	import wareItem from "../../components/wareItem/wareItem.vue"
	export default {
		onLoad(options) {

			this.height = getApp().globalData.pageHeight;
			console.log(options);
			this.content = options.categoryName;
			uni.setNavigationBarTitle({
				title: this.content
			});
			this.header.Authority = uni.getStorageSync("Authority");
			this.getHotGoods(this.content,this.currentPage)
			
		},
		onReachBottom(e) {
			this.getHotGoods(this.content,this.currentPage++)
		},
		onPullDownRefresh() {
			uni.showLoading({
				title: '加载中..',
				mask: true
			});
			this.currentPage = 1;
			this.list = []
			this.getHotGoods(this.content,this.currentPage)
		},
		data() {
			return {
				height: 0,
				content: '',
				currentPage: 1,
				header: {
					Authority: ''
				},
				list: []
			}
		},
		components: {
			wareItem
		},
		methods: {
			//去商品详情页
			toDetail(item) {
				console.log('执行')
				let details = {}
				// let 
				details.itemSourceId = item.itemSourceId;
				details.itemDesc = item.itemDesc;
				details.ticket = item.ticket;
			
				let id = item.id;
				uni.navigateTo({
					url: '../wareDetail/wareDetail?item=' + JSON.stringify(details),
					fail(error) {
						console.log('错误',error)
					}
				});
			},
			//对商品数据进行处理
			dataChange(data) {
				let result = []
				data.forEach((item, index) => {
					// console.log(item)
					let newPrice = item.itemVoucherPrice;
					let oldPrice = item.itemPrice;
					let down = oldPrice - newPrice;
					if (down >= 0) {
						item.ticket = down.toFixed(1)
					} else {
						item.ticket = 0
					}
			
					// //对图片数据处理，若图片地址错误更换为替代图片
					uni.getImageInfo({
						src: item.itemPictureMax,
						success: function(image) {},
						fail(error) {
							// console.log('错误信息', error)
							item.itemPictureMax = 'http://1.14.247.152:9797/images/nopic.png'
						}
					});
					result.push(item);
			
				})
				return result;
			},
			//获取热门商品
			getHotGoods(content, currentPage) {
				uni.showLoading({
					title: '加载中..',
					mask: true
				});
				uni.request({
					url: 'https://api.gzmiyuan.com/api/goods/center/jsonp/search?itemSource=1&keywords=' +
						content + '&searchType=1&coupon=1&page=' + currentPage +
						'&rows=10&sort=itemIndex&order=desc&supplierCode=&inviteCode=YFRPEB', //仅为示例，并非真实接口地址。

					success: (res) => {
						console.log(res.data, '蜜源数据');
						if (res.data.code == "B00000") {
							console.log(res.data.data.data, '置顶数据');
							this.list.push(...this.dataChange(res.data.data.data));
							uni.hideLoading();
						}

					},
					fail(error) {
						// console.log('错误', error)
					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		// background-color: pink;
		// .like_list {
		padding: 0rpx 20rpx 20rpx 20rpx;
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		width: 100%;
		background-color: #f8f8f8;

		.list_item {
			width: 48%;
			// height: 400rpx;
			background-color: #ffffff;
			margin-bottom: 30rpx;
			border-radius: 20rpx;
			padding-bottom: 10rpx;

		}

		// &>.list_item:nth-child(1),
		// &>.list_item:nth-child(2) {
		// 	margin-top: 20rpx;
		// }

		// }
	}
</style>
